<template>
  <div class="order" style="padding:30px;">
    <el-card shadow="never">
      <div slot="header">
        <span>条件查询</span>
      </div>
      <el-form ref="orderSearchForm" :model="orderSearch" label-width="80px" size="mini">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="订单编号">
              <el-input v-model="orderSearch.orderSn" placeholder="请填写订单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="订单类型">
              <!-- orderType (integer, optional): 订单类型：0->正常订单；1->秒杀订单 , -->
              <el-select v-model="orderSearch.orderType" value-key="desc" placeholder="订单类型" clearable filterable>
                <el-option
                  v-for="item in orderType"
                  :key="item.label"
                  :label="item.desc"
                  :value="item.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="支付方式">
              <!-- payType (integer, optional): 支付方式：0->未支付；1->支付宝；2->微信 , -->
              <el-select v-model="orderSearch.payType" value-key="desc" placeholder="支付方式" clearable filterable>
                <el-option
                  v-for="item in payType"
                  :key="item.label"
                  :label="item.desc"
                  :value="item.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="订单状态">
              <!-- status (integer, optional): 订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->退货；5->无效订单 -->
              <el-select v-model="orderSearch.status" value-key="desc" placeholder="订单状态" clearable filterable>
                <el-option
                  v-for="item in status"
                  :key="item.label"
                  :label="item.desc"
                  :value="item.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top:20px;" :gutter="20">
          <el-col :span="2" :offset="20">
            <el-button type="primary" size="small" @click="searchOrder">搜索</el-button>
          </el-col>
          <el-col :span="2" :offset="0">
            <el-button type="default" size="small" @click="resetSearchForm">重置</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-card>
    <el-card style="margin-top:30px;" class="box-card">
      <el-table v-loading="loading" :data="orderList" border stripe>
        <el-table-column
          fixed="left"
          type="index"
          label="#"
          align="center"
          width="50"
        />
        <el-table-column
          label="订单编号"
          prop="orderSn"
          width="200"
          align="center"
        />
        <el-table-column
          label="支付方式"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.payType===0" class="icon-box">
              <el-button type="info" size="mini">未支付</el-button>
            </div>
            <div v-if="scope.row.payType===1" class="icon-box" style="background:#67c23a;">
              <i class="iconfont icon-zhifubaozhanghu icon" />
            </div>
            <div v-if="scope.row.payType===2" class="icon-box" style="background:#409eff;">
              <i class="iconfont icon-weixin icon" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="订单状态"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-button :type="status[scope.row.status].type" :plain="status[scope.row.status].plain" size="small">{{ status[scope.row.status].desc }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="添加时间"
          prop="createTime"
          width="170"
          align="center"
        />
        <el-table-column
          label="用户账号"
          prop="memberUsername"
          width="170"
          align="center"
        />
        <el-table-column
          label="订单类型"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="success" plain size="small">{{ orderType[scope.row.orderType?scope.row.orderType:0].desc }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="note"
          width="150"
          align="center"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-button style="width:80%;" type="primary" size="small" @click="goToOrderDetail(scope.row.id)"><i style="margin-right:5px;" class="el-icon-view" />查看订单</el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top:10px;"
        :current-page="pagination.start"
        :page-sizes="pagination.sizes"
        :page-size="pagination.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>

<script>
import {
  findOrdersByPage as findOrdersByPageApi
} from '@/api/order/list'
import mix from '@/mixin'
export default {
  name: 'OrderList',
  mixins: [mix],
  data() {
    return {
      loading: false,
      orderList: [], // 订单列表
      orderSearch: {}, // 订单搜索项
      orderType: [
        {
          label: 0,
          desc: '正常订单'
        },
        {
          label: 1,
          desc: '秒杀订单'
        }
      ],
      payType: [
        {
          label: 0,
          desc: '未支付'
        },
        {
          label: 1,
          desc: '支付宝'
        },
        {
          label: 2,
          desc: '微信'
        }
      ]
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 跳转到订单详情
    goToOrderDetail(id) {
      this.$router.push({ name: 'OrderDetail', params: { orderSn: id }})
    },
    // 搜索订单
    searchOrder() {
      this.pagination.start = 1
      this.getOrderList()
    },
    // 重置搜索表单
    resetSearchForm() {
      this.orderSearch = {}
      this.pagination.start = 1
      this.getOrderList()
    },
    // 改变limit一页多少条数据
    handleSizeChange(e) {
      this.pagination.limit = e
      this.pagination.start = 1
      this.getOrderList()
    },
    // 改变当前页数start
    handleCurrentChange(e) {
      this.pagination.start = e
      this.getOrderList()
    },
    // 获取订单列表
    getOrderList() {
      this.filterNullParamsObject(this.orderSearch)
      var data = null
      this.loading = true
      if (this.passForm) {
        data = [this.pagination.start, this.pagination.limit, this.orderSearch]
      } else {
        data = [this.pagination.start, this.pagination.limit]
      }

      findOrdersByPageApi(...data)
        .then(res => {
          const { success, message, data } = res
          if (success) {
            this.loading = false
            this.orderList = data.rows
            this.pagination.total = data.total
            this.passForm = false
          } else {
            this.loading = false
            this.$message.error(message)
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.order{
  .icon-box{
    margin: 0 auto;
    width: 56px;
    border-radius: 10px;
    .icon{
      line-height: 30px;
      font-size: 24px;
      color: #fff;
    }
  }

}
</style>
